package utils;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.PointerInput.Kind;
import org.openqa.selenium.interactions.PointerInput.MouseButton;
import org.openqa.selenium.interactions.PointerInput.Origin;
import org.openqa.selenium.interactions.Sequence;
import org.openqa.selenium.Point;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;

import io.appium.java_client.android.AndroidDriver;

import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class BaseAction {
	public AndroidDriver driver;
	public Actions action;

	public BaseAction(AndroidDriver driver) {
		this.driver = driver;
		action = new Actions(driver);

	}

	public String getToastMsg() {

		return driver.findElement(AppiumBy.xpath("//*[@class='android.widget.Toast']")).getText();

	}

	public void click(AppiumBy by) {
		driver.findElement(by).click();
	}

	public void click(WebElement elemnet) throws InterruptedException {
		elemnet.click();
		Thread.sleep(3000);
	}

	// 
	public void longPress(WebElement ele) {
		action.clickAndHold(ele).perform();
	}

	// 
  	public void swipe(int startX, int startY, int endX, int endY) {

		PointerInput finger = new PointerInput(Kind.TOUCH, "finger1");
		Sequence swipe = new Sequence(finger, 0);
		swipe.addAction(finger.createPointerMove(Duration.ZERO, Origin.viewport(), startX, startY));
		swipe.addAction(finger.createPointerDown(MouseButton.LEFT.asArg()));
		swipe.addAction(finger.createPointerMove(Duration.ofMillis(1000), Origin.viewport(), startX, endY));
		swipe.addAction(finger.createPointerUp(MouseButton.LEFT.asArg()));
		driver.perform(Arrays.asList(swipe));
	}

	// 
	public void swip(WebElement fromEle, WebElement toEle) {
		fromEle.getRect();
		fromEle.getLocation();
	

	}
	// 

	public void drag(WebElement from, WebElement to) {

		action.clickAndHold(from).moveToElement(to).release().perform();

	}

	public void setValue(AppiumBy by, String value) {
		driver.findElement(by).clear();
		driver.findElement(by).sendKeys(value);

	}



	public void type(WebElement element, String context) {
		element.sendKeys(context);
	}

	public void typeAndClear(WebElement element, String context) {
		element.clear();
		element.sendKeys(context);
	}

	
}
